//index.js
//获取应用实例

import {
  Apilogin,
  ApiCaptchaCode,
  ApiCaptchaCodeLogin,
  ApiGetSet
} from "../../api/index.js"
const app = getApp()
let {
  baseUrl,
  commonUpdate,
  commonUpload
} = app.globalData
let username = '13164640637'
let password = '123456'
let code = ''
Page({
  data: {
    baseUrl,
    username: '13164640637',
    password: '123456',
    code: '',
    clientHeight: '',
    countdown: 0,
    active: false,
    navBarHeight: app.globalData.navBarHeight,
    menuRight: app.globalData.menuRight,
    menuBotton: app.globalData.menuBotton,
    menuTop: app.globalData.menuTop,
    menuHeight: app.globalData.menuHeight,
    menuWidth: app.globalData.menuWidth,
  },
  onLoad() {
    var that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          clientHeight: res.windowHeight
        });
      }
    })
  },
  onShow() {
    this.getSet()
  },

  privacyAgreement(e) {
    let path = baseUrl + '/' + e.currentTarget.dataset.v + '.pdf'
    wx.showLoading({
      title: '加载中...',
      success: () => {
        wx.downloadFile({
          url: path,
          success: (res) => {
            const filePath = res.tempFilePath
            wx.hideLoading()
            this.openView(filePath)
          },
          fail: () => {
            wx.showToast({
              title: '文件下载失败！',
              icon: "error"
            })
          }
        })
      }
    })
  },
  privacyAgreement1(e) {
    let path = baseUrl + '/' + e.currentTarget.dataset.v + '.pdf'
    wx.showLoading({
      title: '加载中...',
      success: () => {
        wx.downloadFile({
          url: path,
          success: (res) => {
            const filePath = res.tempFilePath
            wx.hideLoading()
            this.openView(filePath)
          },
          fail: () => {
            wx.showToast({
              title: '文件下载失败！',
              icon: "error"
            })
          }
        })
      }
    })
  },

  openView(path) {
    wx.openDocument({
      filePath: path,
      success: function (res) {

      },
      fail: () => {
        wx.showToast({
          title: '文件打开失败！',
          icon: "error"
        })
      }
    })
  },

  onChange(e) {
    this.setData({
      active: !this.data.active
    })
  },
  onLogin() {
    wx.clearStorage()
    let that = this
    this.setData({
      loading: true
    })
    /*    wx.switchTab({
         url: '/pages/index/index'
       }) */
    if (this.data.active) {
      wx.setEnable1v1Chat({
        enable: true
      })
      wx.login({
        success: (res) => {
          Apilogin({
            code: res.code
          }).then(res => {
            /*    app.setToken(res.data.token, res.data) */
            wx.setStorageSync('token', res.data.token)
            wx.setStorageSync('loginType', "patient")
            app.setUserInfo(res.data)
            wx.switchTab({
              url: '/pages/index/index'
            })
          }).then(res => {
            app.setWebScoket()
          })
        },
        fail: (err) => {
          wx.showToast({
            title: '获取code失败',
            icon: "none"
          })
        }
      })
    } else {
      wx.showModal({
        title: "请阅读并勾选《服务协议和隐私协议》！",
        icon: 'error',
        showCancel: false
      })
      this.setData({
        loading: false
      })
    }

  },

  //获取优化
 /*  privacyAgreement() {
    wx.navigateTo({
      url: '/components/privacyAgreement/index',
    })
  }, */

  //autonomousSchedulingSetting
  getSet(){
    ApiGetSet().then(res => {
      this.setData({
        getSet: res.data
      })
    })
  },
  //护理人员登录
  goDoctor() {
    console.log('11')
    wx.navigateTo({
      url: '/pagesDoctor/doctorLogin/index',
    })
  },

  returnIndex() {
    wx.reLaunch({
      url: '/pages/index/index',
    })
  },
  //获取输入款内容
  content(e) {
    username = e.detail
  },
  password(e) {
    password = e.detail
  },
  code(e) {
    code = e.detail
  },
  //登录事件
  goadmin(val) {
    if (val.currentTarget.dataset.v == 'code') {
      this.getCodeLogin()
    } else {
      this.getPassword()
    }
  },
  //验证码登录
  getCodeLogin() {
    let query = {
      phone: username,
      code: code,
    }
    ApiCaptchaCodeLogin(query).then(res => {
      app.setToken(res.data.token, res.data)
      wx.reLaunch({
        url: '/pages/index/index',
      })
    })
  },
  //密码登录
  getPassword() {
    let query = {
      phone: username,
      password: password,
    }
    Apilogin(query).then(res => {
      app.setToken(res.data.token, res.data)
      wx.reLaunch({
        url: '/pages/index/index',
      })
    })
  },
  //获取验证码
  getCaptchaCode() {
    ApiCaptchaCode({
      phone: username,
    }).then(res => {
      this.setData({
        countdown: 60,
      }, () => {
        this.getCountdown()
      })
    })
  },
  getCountdown() {
    this.timer = setInterval(_ => {
      const countdown = this.data.countdown - 1
      if (countdown < 1) {
        clearInterval(this.timer)
        this.timer = null
      }
      this.setData({
        countdown
      })
    }, 1000)
  },
  //tab切换
  onChangeTab(e) {
    this.setData({
      tabLabel: e.detail.name
    })
    username = ''
    password = ''
    code = ''
    this.setData({
      username: '',
      password: '',
      code: '',
    })
  },
})